package org.dfl.core.model.transitions;

import org.dfl.messages.Messages;

import java.util.Iterator;
import java.util.Map;

import org.dfl.core.data.types.Type;
import org.dfl.core.data.types.TypeContainer;
import org.dfl.core.data.types.TypeManager;
import org.dfl.core.model.TransitionExecutionException;


/**
 * this transition checks equals of 2 input elements of the same type
 * @author Piotr Wlodarczyk
 *
 */ 
public class EqualTransition extends FixedInputsTransition {

	@Override
	protected Type getDefaultOutputDefinition() {
		return TypeManager.getBooleanType();
	}
	
	@Override
	protected Object fire(Map<String, Object> inputMap) throws TransitionExecutionException {
		Iterator<String> iter = inputMap.keySet().iterator();
		Object v1 = inputMap.get(iter.next());
		Object v2 = inputMap.get(iter.next());
		
		return ((v1==null && v2==null) || (v1!=null && v1.equals(v2)));
	}

	@Override
	public String getDefaultDescription() {
		return Messages.EqualTransition_description;
	}

	@Override
	protected void initInputDefinitions() {
		try {
			this.addInputDefinition("in1", TypeManager.getSimpleAnyType());
			this.addInputDefinition("in2", TypeManager.getSimpleAnyType());
			// connect inputs to be sure that they have the same type!
			Map<String, TypeContainer> inputs = getInputsDefinition();
			inputs.get("in1").connectWith(inputs.get("in2"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}